<?php
	
	include_once "inc/defines.php";
	include_once "treenode.php";
	
	session_start();
	$unquoted = array("coefficient", "id_form", "id_sem", "id_ue", "id_parcours", "id_module", "id_examen", "id_grptd", "id_grptp", "id_etud"); //doit lister toutes les colonnes dont la valeur ne doit pas etre inseree entre guillemets
	switch($_POST["button"]) {
		case "Appliquer":
			unset($_POST["button"]);
			$table = $_POST["table"];
			unset($_POST["table"]);
			$a = array_keys($_POST);
			$idColumn = $a[0];
			$id = $_POST[$idColumn];
			unset($_POST[$idColumn]);
			$sqlQuery = "UPDATE ".$table." SET ";
			$it = new CachingIterator(new ArrayIterator($_POST));
			foreach($it as $column=>$value) {
				if(!in_array($column, $unquoted))
					$sqlQuery .= $column."=\"".$value."\""; //pour les strings, la valeur doit etre entre guillemets dans la requete
				else
					$sqlQuery .= $column."=".$value;
				if($it->hasNext())
					$sqlQuery .= ", ";
			}
			if(!in_array($idColumn, $unquoted))
				$sqlQuery .= " WHERE ".$idColumn."=\"".$id."\";";
			else
				$sqlQuery .= " WHERE ".$idColumn."=".$id.";";
			break;
		case "Supprimer":
			unset($_POST["button"]);
			$table = $_POST["table"];
			unset($_POST["table"]);
			$a = array_keys($_POST);
			$idColumn = $a[0];
			$id = $_POST[$idColumn];
			if(!in_array($idColumn, $unquoted))
				$sqlQuery = "DELETE FROM ".$table." WHERE ".$idColumn."=\"".$id."\";";
			else
				$sqlQuery = "DELETE FROM ".$table." WHERE ".$idColumn."=".$id.";";
			break;
		case "Ajouter":
			unset($_POST["button"]);
			$table = $_POST["table"];
			unset($_POST["table"]);
			$sqlQuery = "INSERT INTO ".$table." VALUES(";
			$it = new CachingIterator(new ArrayIterator($_POST));
			foreach($it as $key=>$value) {
				if(in_array($key, $unquoted))
					$sqlQuery .= $value;
				else
					$sqlQuery .= "\"".$value."\"";
				if($it->hasNext())
					$sqlQuery .= ", ";
			}
			$sqlQuery .= ");";
			break;
		case "suppr_tous":
			$id = $_POST["id"];
			$sqlQuery = "DELETE FROM ".ETUDIANT_TABLE." WHERE id_grptp=".$id.";";
			break;
		case "depl_tous":
			$id = $_POST["id"];
			$tp = $_POST["id_grptp"];
			$sqlQuery = "UPDATE ".ETUDIANT_TABLE." SET id_grptp=".$tp." WHERE id_grptp=".$id.";";
			break;
	}
	if(isset($sqlQuery)) {
		$db = new PDO(SERVER.":host=".HOST.";dbname=".BASE, USER, PASS);
		$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		try {
			$st = $db->prepare($sqlQuery);
			$st->execute();
		} catch(PDOException $e) {
			$code = $e->getCode();
			if($code == '23000')
				$_SESSION["sqlError"] = "Cet identifiant existe d&eacute;j&agrave;.";
			else if($code == '42S22')
				$_SESSION["sqlError"] = "Le coefficient doit &ecirc;tre un nombre d&eacute;cimal.";
			else
				$_SESSION["sqlError"] = "Erreur d'ex&eacute;cution de la requ&ecirc;te SQL. Si vous &ecirc;tes en train d'ajouter un examen, le coefficient doit &ecirc;tre exprim&eacute; avec un point et non une virgule.";
			header('Location: '.$_SERVER['HTTP_REFERER'], true, 302); //revenir a la page precedante en rafraichissant la page
		}
		$tree1 = unserialize($_SESSION["examTree1"]);
		$tree1->refresh();
		$_SESSION["examTree1"] = serialize($tree1);
		$tree2 = unserialize($_SESSION["examTree2"]);
		$tree2->refresh();
		$_SESSION["examTree2"] = serialize($tree2);
		$tree3 = unserialize($_SESSION["etuTree1"]);
		$tree3->refresh();
		$_SESSION["etuTree1"] = serialize($tree3);
	}
	header('Location: '.$_SERVER['HTTP_REFERER'], true, 302);
	
?>
